package com.example.patient.service.impl;

import cn.hutool.core.util.IdUtil;
import com.example.common.entity.CheckupProduct;
import com.example.common.entity.CheckupReserve;
import com.example.common.entity.CheckupType;
import com.example.common.support.ApiException;
import com.example.common.support.Result;
import com.example.patient.constans.CheckupReserveStatus;
import com.example.patient.dto.resp.CheckupProductList;
import com.example.patient.dto.resp.CheckupReserveList;
import com.example.patient.dto.resp.GetCheckTypeList;
import com.example.patient.mapper.CheckupProductMapper;
import com.example.patient.mapper.CheckupReserveMapper;
import com.example.patient.mapper.CheckupTypeMapper;
import com.example.patient.service.CheckupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @ClassName CheckupServiceImpl
 * @Author wengjt
 * @Date 2021/5/8 16:03
 * @Description
 **/
@Service
public class CheckupServiceImpl implements CheckupService {
    @Resource
    private CheckupTypeMapper checkupTypeMapper;
    @Resource
    private CheckupProductMapper checkupProductMapper;
    @Resource
    private CheckupReserveMapper checkupReserveMapper;

    /**
     * 获取检查类型列表
     *
     * @Author wengjt
     * @Date 16:09 2021/5/8
     */
    @Override
    public GetCheckTypeList getCheckTypeList(Integer ctId, Integer showAble, Integer pageNum, Integer pageSize) {
        GetCheckTypeList result = new GetCheckTypeList();
        PageInfo<CheckupType> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            checkupTypeMapper.multipleSearch(ctId, showAble);
        });
        result.setCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 获取体检套餐列表
     *
     * @Author wengjt
     * @Date 16:10 2021/5/8
     */
    @Override
    public CheckupProductList getCheckupProductList(String cpName, Long ctId, Integer showAble, Integer recommend, Integer pageNum, Integer pageSize) {
        CheckupProductList result = new CheckupProductList();
        PageInfo<CheckupProduct> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            checkupProductMapper.multipleSearch(cpName, ctId, showAble, recommend);
        });
        result.setList(pageInfo.getList());
        result.setCount(pageInfo.getTotal());
        return result;
    }

    /**
     * 获取体检详情
     *
     * @Author wengjt
     * @Date 16:10 2021/5/8
     */
    @Override
    public CheckupProduct getInfo(Long cpId) {

        return checkupProductMapper.findOneById(cpId);
    }

    /**
     * 预约体检
     *
     * @Author wengjt
     * @Date 16:11 2021/5/8
     */
    @Override
    public Object appointmentCheckup(Long uid,Long cpId, Long pid, Date reserve, String linkman, String linkTel) {
        CheckupProduct checkupProduct = checkupProductMapper.findOneById(cpId);
        if (checkupProduct.getDisable()==1){
            throw new ApiException(400,"不能预约已经删除的体检项目");
        }
        if (checkupProduct.getShowAble()==0){
            throw new ApiException(400,"不能预约未上架的体检项目");
        }
        CheckupReserve checkupReserve = new CheckupReserve();
        checkupReserve.setCrOrderNo(IdUtil.simpleUUID());
        checkupReserve.setPId(pid);
        checkupReserve.setProductId(cpId);
        checkupReserve.setUid(uid);
        checkupReserve.setReserve(reserve);
        checkupReserve.setLinkMan(linkman);
        checkupReserve.setLinkTel(linkTel);
        checkupReserve.setRStatus(CheckupReserveStatus.UN_PROCESS);
        checkupReserve.setCreateTime(new Date());
        checkupReserve.setDisable(0);
        checkupReserveMapper.insertOne(checkupReserve);
        return null;
    }
    /**
     *获取病人的
     *    
     * @Author wengjt
     * @Date 10:58 2021/5/14
     */
    @Override
    public CheckupReserveList getCheckupReserveList(Long uid, int rStatus, Integer pageNum, Integer pageSize) {
        CheckupReserveList result = new CheckupReserveList();
        PageInfo<CheckupReserve> pageInfo = PageHelper.startPage(pageNum, pageSize).doSelectPageInfo(() -> {
            checkupReserveMapper.findPageByPid(uid, rStatus);
        });
        result.setCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }
}
